
package utils;

import FrameWork.Global;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;
import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import static javax.media.opengl.GL.GL_LINEAR;
import static javax.media.opengl.GL.GL_TEXTURE_2D;
import static javax.media.opengl.GL.GL_TEXTURE_MAG_FILTER;
import static javax.media.opengl.GL.GL_TEXTURE_MIN_FILTER;
import javax.media.opengl.GL2;
import javax.media.opengl.GLException;
import javax.media.opengl.GLProfile;

/**
 *
 * @author trong_000
 */
public class TextureLoader {
        static TextureLoader _instance = null;
        
        private TextureLoader(){}
        
        public static TextureLoader getIns(){
            if(_instance == null){
                _instance = new TextureLoader();
            }
            return _instance;
        }
        
        /**
         * Hàm load texture từ file ảnh png
         * @param fileName
         * @return 
         */
        public Texture Load(String fileName){
            GL2 gl = Global.drawable.getGL().getGL2();
            Texture tt=null;
            TextureData ttdt;
            InputStream is = null;
            BufferedImage tBufferedImage = null;
             // Load texture from image
            try {
                File f = new File(fileName);
                tt = TextureIO.newTexture(f, false);
                //tBufferedImage = ImageIO.read(getClass().getClassLoader().getResource(fileName));
                //tt = AWTTextureIO.newTexture(GLProfile.getDefault(), tBufferedImage, false);
                //tt = TextureIO.newTexture(
               //getClass().getClassLoader().getResource(fileName), // relative to project root 
               //false, "png");
         // Use linear filter for texture if image is larger than the original texture
                gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
         // Use linear filter for texture if image is smaller than the original texture
                gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

         // Texture image flips vertically. Shall use TextureCoords class to retrieve
         // the top, bottom, left and right coordinates, instead of using 0.0f and 1.0f.
            } catch (GLException e) {
                 e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return tt;
        }
}
